package com.gec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.mapper.SysLoginLogMapper;
import com.gec.model.system.SysLoginLog;
import com.gec.model.vo.SysLoginLogQueryVo;
import com.gec.system.service.SysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {
    @Autowired
    private SysLoginLogMapper sysLoginLogMapper;

    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setStatus(status);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setMsg(message);
        this.sysLoginLogMapper.insert(sysLoginLog);

    }

    @Override
    public IPage<SysLoginLog> selectPage(Long page, Long limit, SysLoginLogQueryVo sysLoginLogQueryVo) {
        //创建page对象
        Page<SysLoginLog> pageParam=new Page(page,limit);
        //获取条件值
        String usernmae=sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();
        //
        QueryWrapper<SysLoginLog> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(usernmae)){
            wrapper.like("username",usernmae);
        }
        if(!StringUtils.isEmpty(createTimeBegin)){
            wrapper.like("create_time",createTimeBegin);
        }
        if(!StringUtils.isEmpty(createTimeEnd)){
            wrapper.like("create_time",createTimeEnd);
        }
        IPage<SysLoginLog> iPage=sysLoginLogMapper.selectPage(pageParam,wrapper);
        return iPage;
    }
    @Override
    public SysLoginLog getById(Long id) {
        return this.sysLoginLogMapper.selectById(id);
    }

    @Override
    public int removeById(Long id) {
        return this.sysLoginLogMapper.deleteById(id);
    }

    @Override
    public int removeIds(List<Long> ids) {
        return this.sysLoginLogMapper.deleteBatchIds(ids);
    }


}
